package cn.tedu.bonfire.passport.controller;

import cn.tedu.bonfire.common.consts.web.HttpConsts;
import cn.tedu.bonfire.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.bonfire.common.web.JsonResult;
import cn.tedu.bonfire.passport.pojo.param.AdminUserLoginParam;
import cn.tedu.bonfire.passport.pojo.param.UserLoginInfoParam;
import cn.tedu.bonfire.passport.pojo.param.UserPhoneLoginParam;
import cn.tedu.bonfire.passport.pojo.vo.UserLoginResultVO;
import cn.tedu.bonfire.passport.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/users")
@Validated
@Api(tags = "1. 单点登录")
public class UserController implements HttpConsts {
    @Autowired
    private IUserService userService;

    public UserController() {
        log.info("创建控制器对象：UserController");
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 100)
    public JsonResult login(@Validated UserLoginInfoParam userLoginInfoParam,
                            @ApiIgnore HttpServletRequest request) {
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginInfoParam);
        String remoteAddr = request.getRemoteAddr();
        String userAgent = request.getHeader(HEADER_USER_AGENT);
        UserLoginResultVO userLoginResultVO = userService.login(userLoginInfoParam,remoteAddr, userAgent);
        return JsonResult.ok(userLoginResultVO);
    }

    @PostMapping("/admin/login")
    @ApiOperation("后台用户登录")
    @ApiOperationSupport(order = 100)
    public JsonResult login(@Validated AdminUserLoginParam adminUserLoginParam,
                            @ApiIgnore HttpServletRequest request) {
        log.debug("开始处理【用户登录】的请求，参数：{}", adminUserLoginParam);
        String remoteAddr = request.getRemoteAddr();
        String userAgent = request.getHeader(HEADER_USER_AGENT);
        UserLoginResultVO userLoginResultVO = userService.adminLogin(adminUserLoginParam,remoteAddr, userAgent);
        return JsonResult.ok(userLoginResultVO);
    }

    @PostMapping("/login/phone")
    @ApiOperation("短信验证码登录")
    @ApiOperationSupport(order = 110)
    public JsonResult loginByPhone(UserPhoneLoginParam userPhoneLoginParam, @ApiIgnore HttpServletRequest request) {
        log.debug("开始处理【短信验证码登录】的请求，参数：{}", userPhoneLoginParam);
        String remoteAddr = request.getRemoteAddr();
        String userAgent = request.getHeader(HEADER_USER_AGENT);
        UserLoginResultVO loginResultVO = userService.loginByPhone(userPhoneLoginParam, remoteAddr, userAgent);
        return JsonResult.ok(loginResultVO);
    }

    @PostMapping("/captcha/phone")
    @ApiOperation("获取登录手机号验证码")
    @ApiOperationSupport(order = 120)
    public JsonResult getPhoneCaptcha(String phone) {
        log.debug("开始处理【获取登录手机号验证码】的请求，参数：{}", phone);
        userService.getCaptchaByPhone(phone);
        return JsonResult.ok();
    }

    @GetMapping("/captcha/code")
    @ApiOperation("生成图形验证码")
    @ApiOperationSupport(order = 130)
    public JsonResult getCodeCaptcha() {
        log.debug("开始处理【获取登录图形验证码】的请求，无参数");
        String base64String = userService.getCodeCaptcha();
        return JsonResult.ok(base64String);
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 200)
    public JsonResult logout(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) {
        log.debug("开始处理【退出登录】的请求，无参数");
        userService.logout(currentPrincipal);
        return JsonResult.ok();
    }


    @PostMapping("/getAuthorities")
    @ApiOperation("获取当事人权限列表")
    @ApiOperationSupport(order = 300)
    public JsonResult getAuthorities(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        log.debug("开始处理【获取当事人权限列表】的请求，无参数");
        String authorities = userService.getAuthorities(currentPrincipal);
        return JsonResult.ok(authorities);
    }

}
